﻿/*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using System.Xml.Linq;
using System.Collections;
using System.Drawing;

namespace HOOMT
{

    class XmlHelper
    {

        private static XmlHelper xmlTool = null;
        //缓存文件的存放路径
        public static String cahePath = "c:\\1.xml";
        private XDocument doc;
        private XDocument xmlroot = null;

        //public enum ElementType{
        //    Method="Method",
        //    Attribute="Attribute"
        //}



        private XmlHelper() { }


        public static XmlHelper GetInstance()
        {
            if (xmlTool == null)
            {
                xmlTool = new XmlHelper();
                xmlTool.InitCahe();
                Console.WriteLine("singleton 1");
                return xmlTool;
            }
            else
            {
                Console.WriteLine("singleton 2");
                return xmlTool;
            }

        }


        private void InitCahe()
        {

            if (File.Exists(cahePath))
            {
                // doc = XDocument.Load(cahePath);
                File.Delete(cahePath);
                this.WirteCahe();
            }
            else
            {
                this.WirteCahe();
            }
        }

        private void WirteCahe()
        {

            doc = new XDocument(
                new XDeclaration("1.0", "utf-8", null),
                new XElement("Info",
                    new XElement("Shapes",
                        new XElement("HighOrderObjectDiagrams"
                //,
                //new XElement("HighOrderObjectDiagram",
                //    new XElement("Id"),
                //    new XElement("Name"),
                //    new XElement("Attributes"),
                //    new XElement("Methods"),
                //    new XElement("Physical",
                //        new XElement("UpLeftpoint"),
                //        new XElement("Width"),
                //        new XElement("Height")

                            //    )
                //)
                        ),
                        new XElement("PrimitiveObjectDiagrams"
                //,
                //new XElement("PrimitiveObjectDiagram",
                //    new XElement("Id"),
                //    new XElement("Name"),
                //    new XElement("Attributes"),
                //    new XElement("Methods"),
                //    new XElement("Physical",
                //        new XElement("UpLeftpoint"),
                //        new XElement("Width"),
                //        new XElement("Height")

                            //    )
                //)
                        )

                    )
                )
            );
            doc.Save(cahePath);
            xmlroot = XDocument.Load(cahePath);

        }
        public void AddDiagramBaseInfo(OriginalDiagram od)
        {
            System.Type type = od.GetType();
            String strType = type.ToString().Substring(22);
        //    Console.WriteLine(type);
            XElement parent = GetParentElement(od);

            XElement existDiagram = this.FindDiagramElement(od);
            if (existDiagram!=null)
            {
                existDiagram.Remove();
            } 
            

            parent.Add(
                    new XElement(strType,
                        new XElement("Id", od.Id),
                        new XElement("Name", od.Name),
                        new XElement("Attributes"),
                        new XElement("Methods"),
                        new XElement("Physical",
                            new XElement("UpLeftpoint",
                                new XAttribute("X", od.UpLeftpoint.X),
                                new XAttribute("Y", od.UpLeftpoint.Y)
                                ),
                            new XElement("Width", od.Width),
                            new XElement("Height", od.Height)
                        )
                    )
                );
            foreach (Attribute attr in od.GetAttributes())
            {
                AddAttribute(attr, od);

            }

            foreach (Method method in od.GetMethods())
            {
                AddMethod(method, od);
            }


            //   Console.WriteLine("111");
            this.Save();
            //   Console.WriteLine("22");
        }


        public static void CleanUpCache()
        {
            File.Delete(cahePath);
        }

        public void LoadXmlFormFile(String srcFilePath)
        {
            File.Copy(srcFilePath, cahePath, true);
        }


        public void AddAttribute(Attribute attr, OriginalDiagram parentDiagram)
        {
            try
            {
                XElement parentElement = this.FindDiagramElement(parentDiagram);

                XElement attributes = parentElement.Element("Attributes");
                attributes.Add(new XElement("Attribute",
                    new XElement("AccessModifier", attr.AccessModifier),
                    new XElement("AttributeName", attr.AttributeName),
                    new XElement("AttributeType", attr.AttributeType),
                    new XElement("InitialValue", attr.InitialValue),
                    new XElement("Description", attr.Description)

                    )
                 );

                xmlroot.Save(cahePath);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        }
        public void AddMethod(Method method, OriginalDiagram parentDiagram)
        {
            try
            {

                XElement methods = this.FindDiagramElement(parentDiagram).Element("Methods");
                methods.Add(new XElement("Method",
                    new XElement("AccessModifier", method.AccessModifier),
                    new XElement("MethodName", method.MethodName),
                    new XElement("ParameterList", method.ParameterList),
                    new XElement("ReturnType", method.ReturnType),
                    new XElement("Description", method.Description)

                    )
                 );
                xmlroot.Save(cahePath);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        }

        //已经废弃
        //public void AddPhysicalInfo(OriginalDiagram od)
        //{
        //    XElement physical;
        //    try
        //    {
        //        XDocument xmlroot = XDocument.Load(cahePath);
        //        System.Type type = od.GetType();
        //        //  Console.WriteLine(type);
        //        String strType = type.ToString().Substring(22);
        //        physical = xmlroot.Element("Info").Element("Shapes").Element(strType + "s").Element(strType).Element("Physical");
        //        physical.Element("UpLeftpoint").Add(new XAttribute("X", od.UpLeftpoint.X));
        //        physical.Element("UpLeftpoint").Add(new XAttribute("Y", od.UpLeftpoint.Y));
        //        physical.SetElementValue("Width", od.Width);
        //        physical.SetElementValue("Height", od.Height);
        //        XElement id = xmlroot.Element("Info").Element("Shapes").Element(strType + "s").Element(strType).Element("Id");
        //        id.SetValue(od.Id);
        //        xmlroot.Save(cahePath);
        //    }
        //    catch (Exception e)
        //    {
        //        System.Console.WriteLine(e.Message);
        //    }
        //}

        public XElement FindDiagramElement(OriginalDiagram od)
        {
            System.Type type = od.GetType();
            //  Console.WriteLine(type);
            String strType = type.ToString().Substring(22);
            XElement parent = this.GetParentElement(od);
            //         Console.WriteLine(parent.ToString());
            IEnumerable<XElement> diagram =
                from el in parent.Elements(strType)
                where (string)el.Element("Id") == od.Id
                select el;

            //           Console.WriteLine(diagram.LongCount());
            if (diagram.LongCount()>0)
            {
                return diagram.ElementAt(0);
            }else{
                return null;
            }
            
        }

        public XElement FindDiagramElementById(String id)
        {


            return null;
        }
        //这个方法有问题
        private XElement GetSubRoot(OriginalDiagram od)
        {
            XDocument xmlroot = XDocument.Load(cahePath);
            System.Type type = od.GetType();
            //  Console.WriteLine(type);
            String strType = type.ToString().Substring(22);
            XElement subRoot = xmlroot.Element("Info").Element("Shapes").Element(strType + "s").Element(strType);
            return subRoot;

        }
        private XElement GetParentElement(OriginalDiagram od)
        {

            System.Type type = od.GetType();
            //  Console.WriteLine(type);
            String strType = type.ToString().Substring(22);
            XElement parentElement = xmlroot.Element("Info").Element("Shapes").Element(strType + "s");
            return parentElement;

        }

        private void Save()
        {
            //    XDocument xmlroot = XDocument.Load(preTreeNode);
            xmlroot.Save(cahePath);
        }
        public void SaveHOOD() { }

        //public void UpdateElements(Object element,OriginalDiagram od)
        //{

        //}
        public int UpdateDiagrams(List<OriginalDiagram> diagrams)
        {

            InitCahe();

            foreach (OriginalDiagram od in diagrams)
            {
                AddDiagramBaseInfo(od);
            }
            return diagrams.Count;
        }

        public HighOrderObjectDiagram GetHighOrderObjectDiagram(XElement hoodElement)
        {
            Console.WriteLine(hoodElement.ToString());
            //   Console.WriteLine(hoodElement.Element("Physical").Element("UpLeftpoint").Attribute("X").ToString());
            int x = int.Parse(hoodElement.Element("Physical").Element("UpLeftpoint").Attribute("X").Value);
            int y = int.Parse(hoodElement.Element("Physical").Element("UpLeftpoint").Attribute("Y").Value);
            Point upLeftPoint = new Point(x, y);
            int width=int.Parse(hoodElement.Element("Physical").Element("Width").Value);
            int height = int.Parse(hoodElement.Element("Physical").Element("Height").Value);

            HighOrderObjectDiagram hood = new HighOrderObjectDiagram(HighOrderObjectDiagram.GetCenterPoint(upLeftPoint,width,height),
                width,height);
            hood.Id = hoodElement.Element("Id").Value;

            hood.AttributeList = this.GetAttrbuteList(hoodElement.Element("Attributes"));
            hood.MethodList = this.GetMethodList(hoodElement.Element("Methods"));
            //IEnumerable<XElement> Attributes =
            //    from el in hoodElement.Elements("Attributes")
            //    select el;
            //foreach (XElement attrEl in Attributes)
            //{
            //    hood.AttributeList.Add(this.GetAttrbute(attrEl));
            //}

            //IEnumerable<XElement> Methods =
            //    from el in hoodElement.Elements("Methods")
            //    select el;
            //foreach (XElement methodEl in Attributes)
            //{
            //    hood.MethodList.Add(this.GetMethod(methodEl));
            //}
            return hood;
        }

        public PrimitiveObjectDiagram GetPrimitiveObjectDiagram(XElement podElement)
        {
            Console.WriteLine(podElement.ToString());
            int x = int.Parse(podElement.Element("Physical").Element("UpLeftpoint").Attribute("X").Value);
            int y = int.Parse(podElement.Element("Physical").Element("UpLeftpoint").Attribute("Y").Value);
            Point upLeftPoint = new Point(x, y);
            int width = int.Parse(podElement.Element("Physical").Element("Width").Value);
            int height = int.Parse(podElement.Element("Physical").Element("Height").Value);

            PrimitiveObjectDiagram pod = new PrimitiveObjectDiagram(PrimitiveObjectDiagram.GetCenterPoint(upLeftPoint, width, height),
                width,height);
            pod.Id = podElement.Element("Id").Value;

            pod.AttributeList = this.GetAttrbuteList(podElement.Element("Attributes"));
            pod.MethodList = this.GetMethodList(podElement.Element("Methods"));
            //IEnumerable<XElement> Attributes =
            //    from el in podElement.Elements("Attributes")
            //    select el;
            //foreach (XElement attrEl in Attributes)
            //{
            //    pod.AttributeList.Add(this.GetAttrbute(attrEl));
            //}

            //IEnumerable<XElement> Methods =
            //    from el in podElement.Elements("Methods")
            //    select el;
            //foreach (XElement methodEl in Attributes)
            //{
            //    pod.MethodList.Add(this.GetMethod(methodEl));
            //}
            return pod;
        }



        public Attribute GetAttrbute(XElement attrElement)
        {
            Attribute attr = new Attribute(attrElement.Element("AccessModifier").Value,
                attrElement.Element("AttributeType").Value,
                attrElement.Element("AttributeName").Value,
                attrElement.Element("InitialValue").Value,
                attrElement.Element("Description").Value);
            return attr;
        }

        public List<Attribute> GetAttrbuteList(XElement attributesElement){
            List<Attribute> attrList=new List<Attribute>();
            IEnumerable<XElement> attributes =
                from el in attributesElement.Elements("Attribute")
                select el;
            if (attributes.LongCount()>=0)
            {
                foreach (XElement attrEl in attributes)
                {
                    attrList.Add(this.GetAttrbute(attrEl));
                }
            }
            
            return attrList;
        }

        public List<Method> GetMethodList(XElement methodsElement)
        {
            List<Method> methodList = new List<Method>();
            IEnumerable<XElement> mehods =
                from el in methodsElement.Elements("Method")
                select el;
            if (mehods.LongCount() >= 0)
            {
                foreach (XElement methodEl in mehods)
                {
                    methodList.Add(this.GetMethod(methodEl));
                }
            }

            return methodList;
        }


        public Method GetMethod(XElement methodElement)
        {
            Method method = new Method(methodElement.Element("AccessModifier").Value,
                methodElement.Element("ReturnType").Value,
                methodElement.Element("MethodName").Value,
                methodElement.Element("ParameterList").Value,
                methodElement.Element("Description").Value);
            return method;
        }

        public List<OriginalDiagram> GetDiagramList(string fileName)
        {
            xmlroot = XDocument.Load(fileName);
            //     Console.WriteLine(xmlroot.ToString());
            List<OriginalDiagram> diagramList = new List<OriginalDiagram>();
            IEnumerable<XElement> hoods =
                from el in xmlroot.Element("Info").Element("Shapes").Element("HighOrderObjectDiagrams").Elements("HighOrderObjectDiagram")
                select el;
            foreach (XElement hoodEl in hoods)
            {
                diagramList.Add(this.GetHighOrderObjectDiagram(hoodEl));
            }

            IEnumerable<XElement> pods =
                from el in xmlroot.Element("Info").Element("Shapes").Element("PrimitiveObjectDiagrams").Elements("PrimitiveObjectDiagram")
                select el;
            foreach (XElement podEl in pods)
            {
                diagramList.Add(this.GetPrimitiveObjectDiagram(podEl));
            }
            return diagramList;
        }

        //public List<OriginalDiagram> LoadDiagramFromFile(string fileName)
        //{
        //    xmlroot = XDocument.Load(fileName);

        //    return this.GetMethodList()
        //}

    }

}*/
